<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item
        label="仓库名称"
        prop="name"
        :class="{ 'is-required': !dataForm.id }"
      >
        <el-input v-model="dataForm.name" placeholder="仓库名称"></el-input>
      </el-form-item>
      <el-form-item
        label="仓库地址"
        prop="address"
        :class="{ 'is-required': !dataForm.id }"
      >
        <el-input v-model="dataForm.address" placeholder="仓库地址"></el-input>
      </el-form-item>
      <el-form-item
        label="仓储费"
        prop="warehousing"
        :class="{ 'is-required': !dataForm.id }"
      >
        <el-input
          v-model="dataForm.warehousing"
          placeholder="仓储费"
        ></el-input>
        元/天/KG
      </el-form-item>
      <el-form-item label="搬运费" prop="truckage">
        <el-input v-model="dataForm.truckage" placeholder="搬运费"></el-input>
      </el-form-item>
      <el-form-item label="负责人" prop="phoneNum">
        <el-input v-model="dataForm.phoneNum" placeholder="负责人"></el-input>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input v-model="dataForm.sort" placeholder="排序"></el-input>
      </el-form-item>
      <el-form-item label="描述" prop="remark">
        <el-input v-model="dataForm.remark" placeholder="描述"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { isEmail, isMobile } from "@/utils/validate";
export default {
  data() {
    var validateWarehousing = (rule, value, callback) => {
      if (!/^\d+$|^\d*\.\d+$/.test(value)) {
        callback(new Error("请输入正确格式的仓储费，只能为数字"));
      }
    };
    var validateTruckage = (rule, value, callback) => {
      if (!/^\d+$|^\d*\.\d+$/.test(value)) {
        callback(new Error("请输入正确格式的搬运费，只能为数字"));
      }
    };
    return {
      parentValue: [],
      value1: "",
      value2: "",
      value: "",
      visible: false,
      roleList: [],
      dataForm: {
        id: 0,
        name: "",
        address: "",
        warehousing: "",
        truckage: "",
        sort: "",
        phoneNum: "",
        remark: "",
        beginNeedGet: "",
        beginNeedPay: "",
        allNeedGet: "",
        allNeedPay: "",
        taxRate: "",
      },
      dataRule: {
        name: [
          { required: true, message: "仓库名称不能为空", trigger: "blur" },
        ],
        address: [
          { required: true, message: "仓库地址不能为空", trigger: "blur" },
        ],
        warehousing: [
          { required: true, message: "仓储费不能为空", trigger: "blur" },
          { validator: validateWarehousing, trigger: "blur" },
        ],
        truckage: [
          { required: true, message: "搬运费不能为空", trigger: "blur" },
          { validator: validateTruckage, trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    init(index, id, pageIndex, pageSize, name, remark) {
      // console.log(index, id, pageIndex, pageSize,name,remark);
      this.dataForm.id = id || 0;
      this.$http({
        url: this.$http.adornUrl("/sys/role/select"),
        method: "get",
        params: this.$http.adornParams(),
      })
        .then(({ data }) => {
          this.roleList = data && data.code === 0 ? data.list : [];
        })
        .then(() => {
          this.visible = true;
          this.$nextTick(() => {
            this.$refs["dataForm"].resetFields();
          });
        })
        .then(() => {
          if (this.dataForm.id) {
            let str = `{"name":"${name}","type":0,"remark":"${remark}"}`;
            var search = encodeURIComponent(str);
            this.$http({
              url: this.$http.adornUrl(`/depot/list?search=${search}`),
              method: "get",
              params: this.$http.adornParams({
                currentPage: pageIndex,
                pageSize: pageSize,
              }),
            }).then(({ data }) => {
              console.log(data.data.list[index]);
              //   if (data.data[index].length == 0) {
              //   return;
              // }
              this.dataForm = data.data.list[index];
              console.log(this.dataForm);
            });
          }
        });
    },

    // 表单提交
    dataFormSubmit() {
      let isRequest = 0;
      this.$refs["dataForm"].validate((valid) => {
        console.log(valid);
        if (valid) {
        } else {
          isRequest = 1;
          return false;
        }
      });
      if (isRequest == 0) {
        this.submit();
      }
    },
    submit() {
      if (!this.dataForm.warehousing) {
        return;
      }
      if (!this.dataForm.truckage) {
        return;
      }
      let info = {
        name: this.dataForm.name,
        address: this.dataForm.address,
        warehousing: this.dataForm.warehousing,
        truckage: this.dataForm.truckage,
        principalName: "",
        principal: "",
        selectType: "principal",
        sort: this.dataForm.sort,
        remark: this.dataForm.remark,
        type: 0,
      };
      let url = this.$http.adornUrl("/depot/add");
      if (this.dataForm.id) {
        url = this.$http.adornUrl(`/depot/update?id=${this.dataForm.id}`);
      }
      this.$http({
        url,
        method: "post",
        data: this.$http.adornData(info, false),
      }).then(({ data }) => {
        console.log(data);
        if (data && data.code === 0) {
          this.$message({
            message: "操作成功",
            type: "success",
            duration: 1500,
            onClose: () => {
              this.visible = false;
              this.$emit("refreshDataList");
            },
          });
        } else {
          this.$message.error(data.msg);
        }
      });
    },
  },
};
</script>
